import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86184
 * Date: 2023-02-20
 * Time: 14:09
 */

class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class StringTransformation {
    //判断结尾或者开头是否是某些字符串
    public static void main(String[] args) {
        String s="abcdefg";
        System.out.println(s.endsWith("fg"));
        System.out.println(s.startsWith("ac"));
    }


    //判断是否包含某些字符或者字符串
    public static void main5(String[] args) {
        String s="abcdefg";
        boolean ret=s.contains("bcde");
        System.out.println(ret);
    }


    //字符串截取
    public static void main4(String[] args) {
        String s="'abcdefg";
        String ret1=s.substring(1);   //截取第一个字符后面的字符串
        System.out.println(ret1);
        String ret2=s.substring(1,4);    //截取第[1,4)的字符串
        System.out.println(ret2);
    }


    //字符串拆分
    public static void main3(String[] args) {
        String s1 = "hello&world&abc";
        String[] ret1 = s1.split("&");   //以&为分割符
        System.out.println(Arrays.toString(ret1));
        String[] ret2 = s1.split("&", 2);    //以&为分割符，只分割成两个
        System.out.println(Arrays.toString(ret2));
        System.out.println("==========特例1=========");
        String s2 = "2023.2.20";
        String[] ret3 = s2.split("\\.");   //. 、+、 * 需要\\转义字符来转义
        for (String x : ret3) {
            System.out.println(x);
        }
        System.out.println("=======特例2==========");
        String s3 = "2023\\02\\20";
        String[] ret4 = s3.split("\\\\");
        for (String x : ret4) {
            System.out.println(x);
        }
        System.out.println("=======一次多个分割符=========");
        String s4 = "name=zhou&gae=19";
        String[] ret5 = s4.split("=|&");
        for (String x:ret5) {
            System.out.println(x);
        }
        System.out.println("=======多次分割=============");
        String[] ret6=s4.split("&");
        for (String x:ret6) {
            String[] ret7=x.split("=");
            for (String x1:ret7) {
                System.out.println(x1);
            }
        }
    }


    //字符串替换
    public static void main2(String[] args) {
        String s = "ababababcegrabcse";
        String ret1 = s.replace('a', 'p');   //将字符a替换成p
        System.out.println(ret1);
        String ret2 = s.replace("abc", "hig");  //将字符串abc替换成hig
        System.out.println(ret1);
        String ret3 = s.replaceAll("ab", "rt");   //将字符串ab替换成rt
        System.out.println(ret3);
        String ret4 = s.replaceFirst("abc", "qwe");  //将第一个abc替换成qwe
        System.out.println(ret4);
    }


    //转换为字符串
    public static void main1(String[] args) {
        String s1 = String.valueOf(123);
        System.out.println(s1);
        String s2 = String.valueOf(12.3);
        System.out.println(s2);
        String s3 = String.valueOf(true);
        System.out.println(s3);
        String s4 = String.valueOf(new Student("周", 18));
        System.out.println(s4);


        //大小写转换
        String s5 = "abedEF周";
        String ret1 = s5.toUpperCase();
        System.out.println(ret1);

        String s6 = "ABCDEFG";
        String ret2 = s6.toLowerCase();
        System.out.println(ret2);

        //字符串转数组
        String s7 = "hello";
        char[] ch = s7.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.println(ch[i]);
        }

        //数组转字符串
        String s8 = new String(ch);
        System.out.println(s8);

        //格式化
        String s9 = String.format("%d %d %d", 2023, 2, 20);
        System.out.println(s9);
    }

}
